<?php
class ManageBaseController extends DefaultBaseController
{
	var $_isAJAX = false;
	var $usertype = USER_TYPE_GUEST;
	
	public function preDispatch() {
		ini_set('memory_limit', '512M');
		$this->_helper->layout()->setLayout($this->getRequest()->getModuleName());
		$this->_isAJAX = $this->getRequest()->isXmlHttpRequest();
		$this->_initNavigation();
    	if (!Zend_Auth::getInstance()->hasIdentity()) {
    		$this->_redirect(HTTP_HOST);
    	}
    	else {
    		$oUser = Zend_Auth::getInstance()->getIdentity();
    		if ($oUser->usertype != $this->usertype) {
    			$this->_redirect($this->routeUserTypeDestination());
    		}
    	}
    	
    	//get breadscrumbs
    	$module_name = $this->getRequest()->getModuleName();
    	$controller_name = $this->getRequest()->getControllerName();
    	$action_name = $this->getRequest()->getActionName();
    	$module = array();
    	switch($module_name){
    		case 'site-manager'	: $module = array('name' => _('Панель менеджера'), 'link' => HTTP_HOST.'/site-manager'); break;
    		case 'site-admin'	: $module = array('name' => _('Панель администратора'), 'link' => HTTP_HOST.'/site-admin'); break;
    	}
    	$controller = array();
    	switch($controller_name){
    		case 'index'			: $controller = array('name' => _('Главная'), 	'link' => $module['link'].'/'); break;
    		case 'special-offers'	: $controller = array('name' => _('Акции'), 	'link' => $module['link'].'/special-offers'); break;
    		case 'articles'			: $controller = array('name' => _('Статьи'), 	'link' => $module['link'].'/articles'); break;
    		case 'news'				: $controller = array('name' => _('Новости'), 	'link' => $module['link'].'/news'); break;
    		case 'country'			: $controller = array('name' => _('Страны'), 	'link' => $module['link'].'/country'); break;
    		case 'object'			: $controller = array('name' => _('Объекты'), 'link' => $module['link'].'/object'); break;
    	} 
    	   	
    	$breadcrumbs = "";
    	(is_array($controller) && array_key_exists('link', $controller))?$breadcrumbs.="<a href='".$controller['link']."' title='".$controller['name']."'>".$controller['name']."</a>":"";

    	$__id = (int)$this->getRequest()->getParam('id', 0);
		$coutry = (int)$this->getRequest()->getParam('country', null);
		if($controller_name == 'country'){
			if($__id > 0){
				$country_ = Obj_Country::getCountry($__id);
			}
			if($action_name == 'edit'){
				$param = (int)$this->getRequest()->getParam('addnew', 0);
				if($param > 0)
					$breadcrumbs .= " &#8594; "._("Добавление новой страны");
				else
					$breadcrumbs .= " &#8594; ".$country_['Name']._(" (режим редактирования)");
			}
		}
		
		if((isset($coutry) && $coutry > 0)){
			$country_ = Obj_Country::getCountry($coutry);
			$breadcrumbs .= " &#8594; <a href='".$controller['link']."/index/country/".$country_['ID']."' title='".$country_['Name']."'>".$country_['Name']."</a>";
		}
		
		$region = (int)$this->getRequest()->getParam('region', null);
		if(isset($region) && $region > 0){
			$region_ = Obj_Region::getRegion($region);
			$country_ = Obj_Country::getCountry($region_['CountryID']);
			$breadcrumbs .= " &#8594; <a href='".$controller['link']."/index/country/".$country_['ID']."' title='".$country_['Name']."'>".$country_['Name']."</a>";
			$breadcrumbs .= " &#8594; <a href='".$controller['link']."/index/region/".$region_['ID']."' title='".$region_['Name']."'>".$region_['Name']."</a>";
		}
		
		$city = (int)$this->getRequest()->getParam('city', null);
		if(isset($city) && $city > 0){
			$city_ = Obj_City::getCity($city);
			$region_ = Obj_Region::getRegion($city_['RegionID']);
			$country_ = Obj_Country::getCountry($city_['CountryID']);
			$breadcrumbs .= " &#8594; <a href='".$controller['link']."/index/country/".$country_['ID']."' title='".$country_['Name']."'>".$country_['Name']."</a>";
			$breadcrumbs .= " &#8594; <a href='".$controller['link']."/index/region/".$region_['ID']."' title='".$region_['Name']."'>".$region_['Name']."</a>";
			$breadcrumbs .= " &#8594; <a href='".$controller['link']."/index/city/".$city_['ID']."' title='".$city_['Name']."'>".$city_['Name']."</a>";
		}
		
		$location = (int)$this->getRequest()->getParam('location', null);
		if(isset($location) && $location > 0){
			$location_ = Obj_Location::getLocation($location);
			$city_ = Obj_City::getCity($location_['CityID']);
			$region_ = Obj_Region::getRegion($location_['RegionID']);
			$country_ = Obj_Country::getCountry($location_['CountryID']);
			$breadcrumbs .= " &#8594; <a href='".$controller['link']."/index/country/".$country_['ID']."' title='".$country_['Name']."'>".$country_['Name']."</a>";
			$breadcrumbs .= " &#8594; <a href='".$controller['link']."/index/region/".$region_['ID']."' title='".$region_['Name']."'>".$region_['Name']."</a>";
			$breadcrumbs .= " &#8594; <a href='".$controller['link']."/index/city/".$city_['ID']."' title='".$city_['Name']."'>".$city_['Name']."</a>";
			$breadcrumbs .= " &#8594; <a href='".$controller['link']."/index/location/".$location_['ID']."' title='".$location_['Name']."'>".$location_['Name']."</a>";
		}
    	$this->view->breadscrumbs = $breadcrumbs;
	}
	
	public function _initNavigation() {
		//need to be in every child
	}
	
	public static function routeUserTypeDestination() {
		if (!($oUser = Zend_Auth::getInstance()->getIdentity())) {
			$oUser = new stdClass();
			$oUser->usertype = 0;
		}
		switch ($oUser->usertype) {
			case USER_TYPE_ADMIN: return HTTP_HOST."/site-admin/";	break;	
			case USER_TYPE_MANAGER: return HTTP_HOST."/site-manager/";	break;	
			default: return HTTP_HOST;
		}
	}
}

